<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" >
<head>
    <th:block th:include="include :: header('修改前期模板')" />
    <th:block th:include="include :: ztree-css" />
    <link rel="stylesheet" th:href="@{/css/myselect.css}"/>
</head>
<body class="white-bg">
    <div class="wrapper wrapper-content animated fadeInRight ibox-content">
        <form class="form-horizontal m" id="form-template-edit" th:object="${lkTemplate}">
            <input name="tempId" id="tempId" th:field="*{tempId}" type="hidden">
            <div class="form-group">    
                <label class="col-sm-3 control-label">模板名称：</label>
                <div class="col-sm-8">
                    <input name="tempName" th:field="*{tempName}" class="form-control" type="text">
                </div>
            </div>
            <div class="form-group">
				<label class="col-sm-3 control-label">事项模板</label>
				<div class="col-sm-8">
					<div id="matterTrees" class="ztree"></div>
				</div>
			</div>
        </form>
    </div>
    <th:block th:include="include :: footer" />
    <th:block th:include="include :: ztree-js" />
    <script type="text/javascript">
        var prefix = ctx + "linkong/template";
        $(function() {
			var url = prefix + "/treeData?tempId=" + $("#tempId").val();
			var options = {
				id: "matterTrees",
		        url: url,
		        check: { enable: true },
		        expandLevel: 0
		    };
			$.tree.init(options);
		});
        $("#form-template-edit").validate({
            focusCleanup: true
        });

        function submitHandler() {
            if ($.validate.form()) {
                var tempId = $("input[name='tempId']").val();
                var tempName = $("input[name='tempName']").val();
                var treeObj=$.fn.zTree.getZTreeObj("matterTrees");
                var nodes=treeObj.getCheckedNodes(true);
                var matterIds="";
                var matterNames="";
                for(var i=0;i<nodes.length;i++){
                    if (nodes[i].text == "事项"){
                        matterIds += nodes[i].id + ",";
                        matterNames += nodes[i].name + ",";
                    }
                }
                matterIds = matterIds.substring(0,matterIds.length-1);
                matterNames = matterNames.substring(0,matterNames.length-1);
                $.ajax({
                    cache : true,
                    type : "POST",
                    url : ctx + "linkong/template/edit",
                    data : {
                        "tempId":tempId,
                        "tempName": tempName,
                        "matterIds": matterIds,
                        "matterNames":matterNames
                    },
                    async : false,
                    error : function(request) {
                        $.modal.alertError("系统错误");
                    },
                    success : function(data) {
                        $.operate.successCallback(data);
                    }
                });
            }
        }
    </script>
</body>
</html>